package com.dongdongshop.service.impl;

import com.dongdongshop.data.Result;
import com.dongdongshop.mapper.TbContentCategoryMapper;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.pojo.TbContentCategoryExample;
import com.dongdongshop.service.TbContentCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@DubboService
public class TbContentCategoryServiceImpl implements TbContentCategoryService {
    @Autowired
    private TbContentCategoryMapper categoryMapper;

    @Override
    public PageResult<TbContentCategory> listCategroy(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        TbContentCategoryExample example = new TbContentCategoryExample();
        if(name != null && !"".equals(name)){
            TbContentCategoryExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%"+ name +"%");
        }
        List<TbContentCategory> list =  categoryMapper.selectByExample(example);
        PageInfo<TbContentCategory> i = new PageInfo<>(list);
        return new PageResult<>(i.getPageNum(),i.getPageSize(),i.getTotal(),i.getPages(),i.getList());
    }

    @Override
    public void addCategroy(TbContentCategory category) {
        categoryMapper.insertSelective(category);
    }

    @Override
    public TbContentCategory selectCatgoryById(Long id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateCategroy(TbContentCategory category) {
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public void deleteCategorys(Long[] ids) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        categoryMapper.deleteByExample(example);
    }

    @Override
    public List<TbContentCategory> getCategroys() {
        return categoryMapper.selectByExample(null);
    }
}
